/*
 * @Author: your name
 * @Date: 2022-01-20 15:58:04
 * @LastEditTime: 2022-01-20 16:32:29
 * @LastEditors: Please set LastEditors
 * @Description: 获取用户token
 * @FilePath: \uniapp\src\store\modules\user.js
 */
export default {
    // 命名空间
    namespaced: true,
    state: {
        token: ""

    },
    getters: {},
    mutations: {
        setToken(state, payload) {
            state.token = payload.token;
        }
    },
    actions: {
        // 获取token
        async fetchToken({ commit }) {
            // store.commit 
            // 1 获取用户信息
            const [err, {
                encryptedData,
                rawData,
                iv,
                signature
            }] =
            await uni.getUserProfile({
                desc: "用于提供更好的服务",
            });

            // 2 获取 code 临时凭据
            const [err2, { code }] = await uni.login();

            // 3 获取token  后端没有写好代码！！ 正常是每一次获取都是ok！！！
            // 以点击两次 为准
            const { token } = await uni.$u.post("/users/wxlogin", {
                encryptedData,
                rawData,
                iv,
                signature,
                code,
            });

            // 4
            // state.token = token;
            commit("setToken", { token });

        }
    }
}